<?php
/**
 * Spaph_DB
 *
 * This is simply a lazy connector class.
 *
 * $dbh = new Spaph_DB($adapter, $config);
 * $conn = $dbh->getConnection();
 *
 * The adapters are Oracle or PDO. Orace returns an oci_connect() resource.
 *
 * PDO requires at least one 'attr' param. Here's an example config:
 *
 *     db.adapter = PDO
 *     db.param.username =
 *     db.param.password =
 *     db.param.dsn = "sqlite:/usr/local/webdata/test.db"
 *     db.param.attr[] = PDO::ATTR_ERRMODE PDO::ERRMODE_EXCEPTION
 *
 */
class Spaph_DB
{
    private $connection  = null;
    private $config      = array();
    private $adapter     = null;

    public function __construct($adapter, $config)
    {
        $default = array();

        // Set up default configurations
        switch($adapter) {
            case 'Oracle':
                $default = array('charset'=> null,'session_mode'=> null);
            break;

            case 'PDO':
                $default = array('username'=> '','password'=> '');
            break;
        }

        $this->adapter = $adapter;
        $this->config  = array_merge($default, $config);
    }

    public function getConfig($conf=false)
    {
        if(false === $conf)
            return $this->config;

        if(array_key_exists($conf, $this->config))
            return $this->config[$conf];

        throw new Exception("The configuration ($conf) does not exist in Spaph_DB.");
    }

    public function getAdapter()
    {
        return $this->adapter;
    }

    public function isConnected()
    {
        return $this->connection ? true : false;
    }

    public function getConnection()
    {
        // Already connected
        if($this->connection) {
            return $this->connection;
        }

        // Create database handle
        switch($this->getAdapter()) {
            case 'Oracle':
                $this->connection = @oci_connect(
                    $this->getConfig('username'),
                    $this->getConfig('password'),
                    $this->getConfig('dbname'),
                    $this->getConfig('charset'),
                    $this->getConfig('session_mode')
                );

                if(!$this->connection) {
                    $e = oci_error();
                    throw new Spaph_DB_Connection_Exception('(ORA-'.$e['code'] .') '. $e['message']);
                }
            break;

            case 'PDO':
                try {
                    $this->connection = new PDO($this->getConfig('dsn'),
                                                $this->getConfig('username'),
                                                $this->getConfig('password'));

                    if($attr = $this->getConfig('attr')) {
                        foreach($attr as $a) {
                        list($name, $value) = preg_split('/[\s,]+/',$a);
                            $this->connection->setAttribute(constant($name), constant($value));
                        }
                    }
                }
                catch(PDOException $e) {
                    throw new Spaph_DB_Connection_Exception($e->getMessage());
                }
            break;
        }

        // Return first connection
        return $this->connection;
    }

    public function close()
    {
        // Close database handle
        switch($this->getAdapter()) {
            case 'Oracle':
                if($this->connection) oci_close($this->connection);
            break;

            case 'PDO':
                $this->connection = null;
            break;
        }

    }
}


